import 'package:bili_caricature/base/base_grid_view.dart';
import 'package:bili_caricature/pages/bloc/cartoon/state.dart';
import 'package:bili_caricature/pages/bloc/widgets/car_toon_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import '../../../base/base_stateful_page.dart';
import '../../../routers/home_router.dart';
import '../../../routers/navigator_utils.dart';
import 'bloc.dart';
import 'event.dart';

class CartoonPage extends BasePage {
  const CartoonPage({super.key});

  @override
  BasePageState<BasePage> getState() {
    return _MessageModulePageState();
  }
}

class _MessageModulePageState extends BasePageState<CartoonPage> {
  /// 请求分页
  int _pageNum = 1;

  /// 列表控制器
  final ScrollController scrollController = ScrollController();

  /// 刷新组建控制器
  final RefreshController refreshController = RefreshController(initialRefresh: false);

  /// 隐藏返回按钮
  @override
  Widget left() {
    return const SizedBox();
  }

  @override
  void initState() {
    super.initState();
    pageTitle = 'bloc-grid';
    pageBgColor = const Color(0xFFF3F4F8);
    isBack = false;
    _getData();
  }

  /// 请求数据
  _getData() {
    String urlStr = '';
    if (_pageNum == 1) {
      urlStr = 'https://run.mocky.io/v3/8d98fef7-634f-4122-a837-8c9ee892365e';
    } else if (_pageNum == 2) {
      urlStr = 'https://run.mocky.io/v3/d415f483-bdbf-445d-ae12-703d1fd01e97';
    } else if (_pageNum == 3) {
      urlStr = 'https://run.mocky.io/v3/a9faaec6-d70f-4365-95b2-6abdd35a6e28';
    } else {
      urlStr = '';
    }
    BlocProvider.of<CartoonBloc>(context).add(
      GetDataEvent(urlStr, {'page': _pageNum}),
    );
  }

  /// 上拉加载
  void _onLoading() {
    _pageNum++;
    _getData();
  }

  /// 下拉刷新
  void _onRefresh() {
    _pageNum = 1;
    _getData();
  }

  Widget mainWidget(CartoonState state) {
    return BaseGridView(
      enablePullDown: true,
      enablePullUp: true,
      onRefresh: _onRefresh,
      onLoading: _onLoading,
      refreshController: refreshController,
      scrollController: scrollController,
      data: state.dataList ?? [],
      padding: EdgeInsets.all(10.h),
      childAspectRatio: 0.7,
      crossAxisSpacing: 10.w,
      mainAxisSpacing: 10.h,
      crossAxisCount: 2,
      bgColor: const Color(0xFFF3F4F8),
      itemBuilder: (context22, index) {
        return CarToonWidget(
          index: index,
          model: state.dataList![index],
          onTap: () {
            NavigatorUtils.push(context, HomeRouter.novelDetailPage,
                arguments: {"imageUrl": state.dataList![index].image});
          },
        );
      },
    );
  }

  void _listener(BuildContext context, CartoonState state) {
    refreshController.refreshCompleted();
    refreshController.loadComplete();
    if (state.isNoMoreDataState == true) {
      refreshController.loadNoData();
    }
  }

  @override
  Widget buildPage(BuildContext context) {
    return BlocConsumer<CartoonBloc, CartoonState>(
      listener: _listener,
      builder: (context, state) {
        return resultWidget(state, (baseState, context) {
          return mainWidget(state);
        }, refreshMethod: () {
          _pageNum = 1;
          _getData();
        });
      },
    );
  }
}
